#include "Common.h"
#include "Application.h"
#include <codecvt>
#include <getopt.h>

#define DEBUG 0

static const char* ShortOptions = "HP";
static const struct option LongOptions[] = {
    {"Seek", required_argument, nullptr, 'a'},
    {"Play", no_argument, nullptr, 'b'},
    {"help", no_argument, nullptr, 'h'},
    {nullptr, 0, nullptr, 0}
};
static bool PreviewVideo = false, Seek = false, Play = false, Stop = false;
static int SeekValue = 0;

void HelpMessage()
{
    cout << "ShotCutTool Parameters Setting:" << endl;
    cout << "-P                   Preveiew video" << endl;
    cout << "--Seek [arguments]   Seek to a specific frame in the video" << endl;
    cout << "--Play               Play the preveiew video" << endl;
    cout << "\nexample: ./ShotCutTool.exe ../Resource/xxx.json -P --Seek 300" << endl;
}

void GetOptions(int& argc, char** argv)
{
    int Option = -1;

    while ((Option = getopt_long(argc, argv, ShortOptions, LongOptions, nullptr)) != -1)
    {
        switch (Option) {
        case 'H':
            {
                HelpMessage();
                break;
            }
        case 'P':
            {
                PreviewVideo = true;
                break;
            }
        case 'a':
            {
                Seek = true;
                SeekValue = std::stoi(optarg);
                break;
            }
        case 'b':
            {
                Play = true;
                break;
            }
        case 'h':
            {
                HelpMessage();
                break;
            }
        default:
            std::cout << "Invalid option" << std::endl;
            break;
        }
    }
    // cout << "PreviewVideo: " << PreviewVideo << ", Seek: " << Seek << ", Play: " << Play << ", Stop: " << Stop << ", SeekValue = " << SeekValue << endl;
}

int main(int argc, char* argv[])
{
    char jsonPath[MAX_PATH];

#if DEBUG
#else
    if (argc < 2)
    {
        return 1;
    }

    strcpy(jsonPath,argv[1]);
    cout << "json文件路径为: " << jsonPath <<endl;
#endif

    char szCurrentDirectory[MAX_PATH], szExeFilePathFileName[MAX_PATH];

    //获取当前进程工作目录

    GetCurrentDirectory(MAX_PATH, szCurrentDirectory);
    GetModuleFileName(NULL, szExeFilePathFileName, MAX_PATH);
    char drive[MAX_PATH], dir[MAX_PATH], fname[MAX_PATH], ext[MAX_PATH];
    _splitpath(szExeFilePathFileName, drive, dir, fname, ext);
    cout << "进程当前工作目录为: " << szCurrentDirectory <<endl;
    cout << "drive: " << drive <<endl;
    cout << "dir: " << dir <<endl;
    cout << "fname: " << fname <<endl;
    cout << "ext: " << ext <<endl;

    //把盘符和文件中间路径组合起来

    ostringstream oss;
    oss<<drive<<dir;

    cout << "切换到新的工作目录为: " << oss.str() <<endl;
    SetCurrentDirectory(string(oss.str()).c_str());
    LoadLibrary("libopenshot.dll");
    LoadLibrary("libjsoncpp-25.dll");
    LoadLibrary("libopenshot-audio.dll");

    QApplication QtApp(argc, argv);

    GetOptions(argc, argv);

#if DEBUG
    PreviewVideo = true;
    string stringPath("E:/QTProject/QTShotCutTool/Resource/shotcut2.json");
    strcpy(jsonPath, stringPath.data());
#endif

    if(!PreviewVideo)
    {
        ShotCutExportVideo* App1 = new ShotCutExportVideo();
        App1->Run(argc, argv, jsonPath);
    }
    else
    {
        ShotCutPreviewVideo* App2 = new ShotCutPreviewVideo();
        App2->show();
        App2->Reader(jsonPath);

        if(Seek)
        {
            App2->Seek(SeekValue);
        }
        else if(Play)
        {
            App2->Play();
        }
    }

    return QtApp.exec();
}
